local skynet = require "skynet"
local filelog = require "filelog"
local timer = require "timer"
require "pubenum"
require "ernnenum"

local tablecmd = {
	server = nil
}

function tablecmd.init(service)
	if tablecmd.server == nil then
		tablecmd.server = service
	end
end

function tablecmd.start(conf, node_id, id)
	local server = tablecmd.server
	if conf == nil or node_id == nil then
		filelog.sys_error(filename .. "conf == nil or node_id == nil")
		return false
	end
	if id ~= nil then
		conf.id = id
	end
	local tablelogic = server:get_logicbyname("tablelogic")
	tablelogic.init_data(server.table_data, conf, node_id)
	skynet.pcall(skynet.retpack, true)
end

function tablecmd.reload()

end

function tablecmd.delete(delreason)
	local server = tablecmd.server
	local table_data = server.table_data
	local roomtablelogic = server:get_logicbyname("tablelogic")
	local rids = roomtablelogic.getallrids(table_data)

	if table_data.delete_table_timer_id > 0 then
		timer.cleartimer(table_data.delete_table_timer_id)
		table_data.delete_table_timer_id = -1
	end
	--检查游戏是否结束
	if delreason ~= "forcedeletegm" and delreason ~= "canceltable" then
		if not roomtablelogic.is_gameend(table_data) then
			table_data.iswilldelete = 1
			return
		end
	end
	--踢出座位上的玩家
	if delreason == "forcedeletegm" then
		roomtablelogic.standupallplayer(table_data,DNEStandupReason.STANDUP_REASON_GM_DELETETABLE)
	elseif delreason == "canceltable" then
		roomtablelogic.standupallplayer(table_data,DNEStandupReason.STANDUP_REASON_CANCELTABLE)
	elseif delreason == "tableempty" then
		roomtablelogic.standupallplayer(table_data,DNEStandupReason.STANDUP_REASON_EMPTY_TABLE)
	elseif delreason == "tableend" then
		roomtablelogic.standupallplayer(table_data,DNEStandupReason.STANDUP_REASON_TABLE_END)
	else
		roomtablelogic.standupallplayer(table_data,DNEStandupReason.STANDUP_REASON_DELETE_TABLE)
	end
	---纪录牌桌战绩数据
	---roomtablelogic.saveGamerecords(table_data)
	--通知dnnode删除table
	--
	if table_data.conf.table_type == DNTableType.TABLE_TYPE_ZUANSI then 
		roomtablelogic.unregister_jackopt()
	end
	skynet.send(".router", "lua", "delete_table", table_data.conf.table_index, rids)
	--删除桌子前清除桌子的状态
	roomtablelogic.clear(table_data)
	--延迟释放桌子
	skynet.sleep(10)
	server:exit_service()
end

function tablecmd.rid_change_diamond(rid, beginvalue, value, endvalue)
	local server = tablecmd.server
	local table_data = server.table_data
	local tablelogic = server:get_logicbyname("tablelogic")
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if not seat then return end
	local noticemsg = {
		money_change_list = {}
	}
	if seat.diamond + value == endvalue then
		seat.diamond = seat.diamond + value
	else
		seat.diamond = endvalue
	end
	seat.playerinfo.diamond = endvalue
	table.insert(noticemsg.money_change_list, { rid = seat.rid, money_type = EMoneyType.DIAMOND, change_value = value, final_value = endvalue})
	local rids = tablelogic.getallrids(table_data)
	server:send_notice_to_players(rids, "moneychangeN", noticemsg, nil, nil)
end


function tablecmd.get_table_conf()
	local server = tablecmd.server
	local table_data = server.table_data
	local tablelogic = server:get_logicbyname("tablelogic")
	--[[
	//创建桌子，桌子的配置，返回桌子配置信息
message table_conf {
	optional int32  table_type      	= 1;	//游戏桌类型： 1 自建钻石桌 2 自建房卡桌
	optional int32	table_mode     		= 2;	//游戏桌模式：1 普通模式  2 扫雷模式
	optional int32  play_game_type   	= 3;	//玩法选择  1 自由抢庄 2 看牌抢庄
	optional int32  xia_zhu_type		= 4;	//下注倍数 1 小倍 2 中倍 3 大倍
	//通用设置
	optional int32	max_player_num		= 5;	//桌子支持玩家位置数 5人
	//只有房卡场
	optional int32	retain_to_num		= 6;	//创建房间局数，只创建房卡需要
	optional int32  cost_type  			= 7;	//房卡花费模式：1 房主自负，2 AA
	optional int32  base_score      	= 8;	//基础分
	optional int32  max_score       	= 9;	//上限分
	//钻石场(数据库里就是钻石)
	optional int32	is_public			= 10;	//是否公开，列表里可以看见（1:公开，0：私密好友）
	optional int32	score_diamond		= 11;	//底注多少钻石，就是一分多少钻石
	optional int32	enter_diamond		= 12;	//进桌多少钻石（客户端计算其实是根据多少分来算的，用户不用随便填写
	optional int32	leave_diamond		= 13;	//离桌多少钻石（计算其实是根据多少分来算的）
	optional int32  tip					= 14;	//房间服务费
	optional string curr_round_code 	= 15;	//牌局唯一ID
}
	--]]
	local conf = {
		table_type = table_data.conf.table_type,
		table_mode = table_data.conf.table_mode,
		play_game_type  = table_data.conf.play_game_type,
		xia_zhu_type = table_data.conf.xia_zhu_type,
		is_open_flush = table_data.conf.is_open_flush,
		max_player_num = table_data.conf.max_player_num,
		retain_to_num = table_data.conf.retain_to_num,
		cost_type = table_data.conf.cost_type,
		base_score = table_data.conf.base_score,
		max_score = table_data.conf.max_score,
		is_public = table_data.conf.is_public,
		score_diamond = table_data.conf.score_diamond,
		enter_diamond = table_data.conf.enter_diamond,
		leave_diamond =  table_data.conf.leave_diamond,
		tip = tablelogic.get_tip() or 10,
		curr_round_code = table_data.conf.curr_round_code,

	}
	----return conf
	skynet.pcall(skynet.retpack, conf)
end

function tablecmd.update_stop_server(flag)
	----filelog.sys_error("-------tablecmd.update_stop_server------", flag)
	local server = tablecmd.server
	local table_data = server.table_data
	local tablelogic = server:get_logicbyname("tablelogic")
	table_data.table_end_flag = flag
	if tablelogic.is_onegameend(table_data) then
		if table_data.delete_table_timer_id > 0 then
            timer.cleartimer(table_data.delete_table_timer_id)
            table_data.delete_table_timer_id = -1
        end
        if table_data.delete_table_timer_id == -1 then
			local noticemsg = {
				table_index = table_data.conf.table_index,
				reason = "tableend",
			}
			table_data.delete_table_timer_id = timer.settimer(1*100, "delete_table", noticemsg)
		end
	end
	---return true
	skynet.pcall(skynet.retpack, true)
end

function tablecmd.clear_player_state(rid)
	local server = tablecmd.server
	local table_data = server.table_data
	local tablelogic = server:get_logicbyname("tablelogic")
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat ~= nil then
		if tablelogic.is_onegameend(table_data) then
			tablelogic.passive_standuptable(table_data, nil, seat, DNEStandupReason.STANDUP_REASON_CLEAR_PLAYER_STATE)
		end
	else
		if table_data.waits[rid] then 
			table_data.waits[rid] = nil 
			if table_data.cur_watch_playernum > 0 then table_data.cur_watch_playernum = table_data.cur_watch_playernum - 1 end
		end
		skynet.call(".router", "lua", "clear_ridinfo", 0, rid, table_data.conf.table_index, table_data.conf.table_type)
	end
	skynet.pcall(skynet.retpack, true)
end

function tablecmd.updatejackinfo(jackopt_num, money_type)
	local server = tablecmd.server
	local table_data = server.table_data
	local tablelogic = server:get_logicbyname("tablelogic")
	if table_data.jackopt_info == nil then table_data.jackopt_info = {} end
	table_data.jackopt_info.jackopt_num = jackopt_num
	table_data.jackopt_info.money_type = money_type
	--[[
	//奖池数量改变通知客户端
message jackoptchangeN {
	required int32 jackopt_num = 1; //
	required int32 money_type = 2;
}
	--]]
	local noticemsg = {
		jackopt_num = table_data.jackopt_info.jackopt_num,
		money_type = table_data.jackopt_info.money_type
	}
	local rids = tablelogic.getallrids(table_data)
	server:send_notice_to_players(rids, "jackoptchangeN", noticemsg, nil, nil)
end

return tablecmd